package pl.koziolekweb.prusakolep.controllers;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

import pl.koziolekweb.prusakolep.App;
import pl.koziolekweb.prusakolep.dao.BugDAO;
import pl.koziolekweb.prusakolep.model.Bug;

/**
 * 
 * @author koziolek
 * 
 * 
 *         Info: $Id: BugListActionController.java 124 2009-02-18 15:08:53Z bjkuczynski $
 */
public class BugListActionController implements Controller {

	public void action(Map<String, Object> params) {
		File db = new File(App.PATH);
		try {
			list(db);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void list(File db) throws NumberFormatException,
			FileNotFoundException, IOException, ClassNotFoundException {
		File[] bugs = db.listFiles();
		BugDAO bugDAO = new BugDAO();
		for (File f : bugs) {
			Bug bug = bugDAO.read(new Long(f.getName()));
			write(bug);
		}
		
	}

	private void write(Bug bug) {
		String desc = "";
		if (bug.getDescription().length() < 30){
			desc = bug.getDescription();
			int l = 30 - desc.length();
			for(int a = l; a>0; a--)
				desc = desc +" ";
		}
		else{
			desc = bug.getDescription().substring(0, 30);
		}			
		System.out.println(bug.getId() + " | "
				+ desc+ " | "
				+ bug.getStatus());
	}
}
